(function () {
  if ((!window || window.kwaigame) && kwaigame) {
    wx = ks;
    tt = ks;
    qq = ks;
    GameGlobal.wx = ks;
    wx.onMessage = function () {};
    wx.getOpenDataContext = function () {};
    wx.sharedCanvas = function () {};
    var e = {
      registerHandler(e) {
        if (this.data) {
          e(this.data);
        } else {
          this.config.parse = e;
        }
      },
      init(e) {
        var t = this;
        this.config = e;
        this.config.EQAPPKEY = e.EQAPPKEY;
        this.config.EQVERSION = e.EQVERSION;
        this.config.EQAUTH = e.EQAUTH;
        this.config.APPID = e.APPID;
        this.config.api_url = "https://oppomini.eqmobi.com/kwaigame/" + this.config.EQAPPKEY + "/" + this.config.EQVERSION + "/api";
        this.config.login_url = "https://oppomini.eqmobi.com/kwaigame/" + this.config.EQAPPKEY + "/" + this.config.EQVERSION + "/login";
        this.config.enable = false;
        this.config.savedir = "";
        this.savefuc = this.setStorage;
        this.loadfuc = this.getStorage;
        this.netpost = this.post;
        try {
          kwaigame.init({
            appid: this.config.APPID,
            appId: this.config.APPID
          });
        } catch (o) {
          this.d("init error:" + o);
        }
        this.config.launcheroption = kwaigame.getLaunchOptionsSync();
        kwaigame.getSystemInfo({
          response(e) {
            t.config.sysinfo = e;
          }
        });
        var n = function n() {
          t.d("need login");
          t.login({
            skipuserinfo: true,
            success() {
              t.config.onLoginned && t.config.onLoginned(t.config.userinfo);
            },
            fail() {}
          });
        };
        try {
          this.loadfuc({
            key: "eq4096user.json",
            success(e) {
              t.d("local read", e);
              if (e) {
                t.config.userinfo = e;
                if (e.openId && "" != e.openId) {
                  t.d("skip login"), e.userinfo && "string" == typeof e.userinfo && (t.config.userinfo.userinfo = JSON.parse(e.userinfo)), t.config.onLoginned && t.config.onLoginned(t.config.userinfo);
                } else {
                  n();
                }
              } else {
                n();
              }
            },
            fail(e) {
              t.d(e);
              n();
            }
          });
        } catch (i) {
          this.d("check login error", i);
          n();
        }
        kwaigame.eqSendEvent = function (e, n, o) {
          undefined === o && (o = null);
          t.sendevent(e, n, o);
        };
        this.syncdata();
        kwaigame.eqLogin = function (e) {
          t.eqlogin(e);
        };
        kwaigame.eqRegisterHandler = function (e) {
          t.registerHandler(e);
        };
        kwaigame.eqGetConfig = function () {
          return t.config;
        };
        kwaigame.eqTouch = function (e) {
          t.touch(e);
        };
        kwaigame.onShow && kwaigame.onShow(function (e) {
          t.onShow(e);
        });
        kwaigame.onHide && kwaigame.onHide(function (e) {
          t.onHide(e);
        });
        kwaigame.onError && kwaigame.onError(function (e, n) {
          t.d("onError", e);
          t.onError(e, n);
        });
      },
      getStorage(e) {
        if (window && !window.cc || !cc) {
          var t = "not support!! getStorage";
          this.d(t);
          e.fail && e.fail(t);
        }
        var n = cc.sys.localStorage.getItem(e.key);
        if (n) {
          e.success && e.success(JSON.parse(n));
        } else {
          e.fail && e.fail("nodata");
        }
      },
      setStorage(e) {
        if (window && !window.cc || !cc) {
          var t = "not support!! setStorage";
          this.d(t);
          e.fail && e.fail(t);
        }
        cc.sys.localStorage.setItem(e.key, JSON.stringify(e.data));
        e.success && e.success("done");
      },
      sendevent(e, t, n) {
        var o = this;
        undefined === n && (n = null);
        var i = this.config;
        this.netpost({
          url: this.config.api_url,
          method: "POST",
          dataType: "json",
          data: {
            event: {
              key: e,
              obj: t
            },
            config: i
          },
          success(e) {
            var t = e.data;
            t.data && t.data.eqop && o[t.data.eqop](t.data[t.data.eqop]);
            t.data && t.data.op && kwaigame[t.data.op](t.data[t.data.op]);
            n && n(t);
          },
          fail(e) {
            o.d("syncdata err=", e);
          }
        });
      },
      codelogin(e) {
        var t = this;
        this.d(e);
        this.config.puserinfo = e.puserinfo;
        this.d(this.config);
        this.netpost({
          method: "POST",
          dataType: "json",
          url: this.config.login_url,
          data: this.config,
          success(n) {
            console.log("codelogin success" + JSON.stringify(n));
            t.d(n);
            if (0 == n.data.ret) {
              var o = n.data.data;
              o.openid && "" != o.openid && (o.openId = o.openid);
              o.userinfo && "string" == typeof o.userinfo && (o.userinfo = JSON.parse(o.userinfo));
              t.savefuc({
                key: "eq4096user.json",
                data: o
              });
              t.config.userinfo = o;
              if (e.skipuserinfo) {
                e.success(n.data);
              } else {
                var i = function i() {
                  kwaigame.getUserInfo({
                    success(n) {
                      console.log("获取用户信息成功：" + JSON.stringify(n));
                      for (var i in n) {
                        o[i] = n[i];
                      }
                      o.orguserinfo = n;
                      t.savefuc({
                        key: "eq4096user.json",
                        data: o
                      });
                      t.config.userinfo = o;
                      e.success(o);
                      t.sendevent("updateuserinfo", o);
                    },
                    fail(t) {
                      console.log("获取用户信息失败: " + JSON.stringify(t));
                      e.fail && e.fail(t);
                    }
                  });
                };
                console.log("codelogin getSetting");
                kwaigame.getSetting({
                  success(t) {
                    if (t.authSetting && t.authSetting["Scope.userInfo"]) {
                      i();
                    } else {
                      kwaigame.authorize({
                        scope: "Scope.userInfo",
                        success() {
                          i();
                        },
                        fail(t) {
                          e.fail && e.fail(t);
                        }
                      });
                    }
                  },
                  fail(t) {
                    console.log("getSetting fail", JSON.stringify(t));
                    kwaigame.authorize({
                      scope: "scope.userInfo",
                      success() {
                        i();
                      },
                      fail(t) {
                        e.fail && e.fail(t);
                      }
                    });
                  }
                });
              }
            } else {
              e.fail(n.data);
            }
          },
          fail(n) {
            console.log("codelogin fail" + JSON.stringify(n));
            t.d(n);
            e.fail(n);
          }
        });
      },
      login(e) {
        var t = this;
        console.log("调用快手官方login");
        kwaigame.login({
          force: !e.skipuserinfo,
          success(n) {
            console.log("调用快手官方login成功");
            t.d(n);
            e.puserinfo = n;
            t.codelogin(e);
          },
          fail(t) {
            console.log("调用快手官方login失败");
            e.fail && e.fail(t);
          }
        });
      },
      eqlogin(e) {
        var t = this;
        e.success = e.success || function () {};
        e.fail = e.fail || function () {};
        this.loadfuc({
          key: "eq4096user.json",
          success(n) {
            if (n.data) {
              var o = n.data;
              if (o.openId && o.nickName) {
                delete n.data.fromuserbutton;
                e.success(n.data);
              } else {
                t.login(e);
              }
            } else {
              t.login(e);
            }
          },
          fail() {
            console.log("读取用户信息缓存失败");
            t.login(e);
          }
        });
      },
      setuserinfo(e) {
        if (this.config.userinfo) {
          for (var t in e) {
            this.config.userinfo[t] = e[t];
          }
          this.sendevent("updateuserinfo", this.config.userinfo);
        } else {
          this.config.userinfo = e;
          this.sendevent("updateuserinfo", this.config.userinfo);
        }
        this.d("local write", this.config.userinfo);
        this.savefuc({
          key: "eq4096user.json",
          data: this.config.userinfo
        });
      },
      getconfig() {
        return this.config;
      },
      d(e, t) {
        this.config.debug && console.log(e, t);
      },
      onShow(e) {
        this.config.start_res = e;
        this.sendevent("onShow", e);
        this.config.onShow && this.config.onShow(e);
      },
      onHide(e) {
        this.sendevent("onHide", e);
        this.config.onHide && this.config.onHide(e);
      },
      onError(e) {
        this.config.error = {
          msg: e
        };
        this.config.error = undefined;
        this.config.onError && this.config.onError(e);
      },
      touchend() {
        this.touchflag = true;
      },
      touch(e) {
        var t = this;
        var n = e.success || function () {};
        var o = e.fail || function () {};
        e.fail = function (n) {
          t.sendevent("跳转失败", e);
          o && o(n);
        };
        e.success = function (o) {
          t.sendevent("跳转成功回调", e);
          n && n(o);
        };
        kwaigame[e.op](e);
      },
      post(e) {
        var t = this;
        var n = new XMLHttpRequest();
        n.onreadystatechange = function () {
          if (4 === n.readyState) {
            if (200 === n.status) {
              var o = n.responseText;
              t.d("post ret", o);
              e.success && e.success({
                data: JSON.parse(o)
              });
            } else {
              e.fail && e.fail({
                status: n.status,
                responseText: n.responseText,
                responseType: n.responseType,
                readyStatus: n.readyState
              });
            }
          }
        };
        n.open(e.method, e.url, true);
        n.setRequestHeader("content-type", "application/json");
        n.setRequestHeader("Accept", "application/json");
        n.send(JSON.stringify(e.data));
      },
      syncdata() {
        var e = this;
        this.netpost({
          method: "POST",
          dataType: "json",
          url: this.config.api_url,
          data: this.config,
          success(t) {
            t.data.eqop && e[t.data.eqop](t.data[t.data.eqop]);
            t.data.op && kwaigame[t.data.op](t.data[t.data.op]);
            t.data.data && e.parseconfig(t.data.data);
          },
          fail(t) {
            e.d("syncdata err=", t);
          }
        });
      },
      parseconfig(e) {
        this.data = e;
        this.config.parse && this.config.parse(e);
      }
    };
    kwaigame.eqapi || (kwaigame.eqapi = e);
  }
})();